package com.knight.concurrent.queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * ArrayBlockingQueue 1、 Copy Right Information : <br>
 * Project : java study <br>
 * Description : 描述<br>
 * Author : Knight<br>
 * Version : 1.0.0 <br>
 * 
 * Date : 2018-10-28<br>
 */
public class ArrayBlockingQueueTst {

	static ArrayBlockingQueue<Object> abq = new ArrayBlockingQueue<Object>(10);

	static void offerTimeObj() {
		for (int i = 0; i < 15; i++) {
			try {
				abq.offer("String" + i, 2000, TimeUnit.MILLISECONDS);
				if (i == 11) {
					abq.take();
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			System.out.println(abq);
		}
	}

	static void offerObj() {
		for (int i = 0; i < 15; i++) {
			abq.offer("String" + i);
			System.out.println(abq);
		}
	}

	static void putObj() {
		for (int i = 0; i < 15; i++) {
			try {
				abq.put("String" + i);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			System.out.println(abq);
		}
	}

	public static void main(String[] args) {
		// offerObj();

//		putObj();

//		offerObj();
		
		for (int i = 0; i < 10; i++) {
			final int _i = i;
			new Thread(()-> {
				try {
					abq.put("String" + _i);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName());
			}).start();
			
		}
		
		
		for (int i = 0; i < 2; i++) {
			new Thread(()-> {
					System.out.println(Thread.currentThread().getName() + "--------"+abq.poll());
			}).start();
			
		}
		
		
		
		
	}

}
